package com.bub.pay.api.service.impl;

import com.bub.pay.api.entity.dto.systemmanagement.SysLogDTO;
import com.bub.pay.api.entity.vo.systemmanagement.SendEmailVO;
import com.bub.pay.api.entity.vo.systemmanagement.SysLogVO;
import com.bub.pay.api.service.SystemManagementService;
import com.bub.pay.core.entity.domain.SysLog;
import com.bub.pay.core.manager.client.ClientManager;
import com.bub.pay.core.manager.system.SysLogManager;
import com.bub.pay.framework.common.config.UserConfig;
import com.bub.pay.framework.common.entity.model.SessionModel;
import com.bub.pay.framework.common.template.EmailTemplate;
import com.bub.pay.framework.common.util.CollectionUtil;
import com.bub.pay.framework.redis.RedisClient;
import com.bub.pay.framework.redis.RedisKeyPrefix;
import com.bub.pay.framework.redis.model.EmailModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author chenchen
 * @Description
 * @Date 2019/9/5
 */
@Service
public class SystemManagementServiceImp implements SystemManagementService {
    @Autowired
    private SysLogManager sysLogManager;
    @Autowired
    private RedisClient redisClient;
    @Resource
    private UserConfig userConfig;
    @Resource
    private ClientManager clientManager;
    private String templateEmailVerificationCode;

    @Override
    public PageInfo<SysLogDTO> getSysLogMsg(SysLogVO sysLogVO) {
        PageHelper.startPage(sysLogVO.getPageNo(),sysLogVO.getPageSize());

        Example example = new Example(SysLog.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(sysLogVO.getCreateName())){
            criteria.andEqualTo("createName",sysLogVO.getCreateName());
        }
        if(Objects.nonNull(sysLogVO.getCreateId())){
            criteria.andEqualTo("createId",sysLogVO.getCreateId());
        }
        if(Objects.nonNull(sysLogVO.getStartDate())){
            criteria.andGreaterThanOrEqualTo("createDate",sysLogVO.getStartDate());
        }
        if(Objects.nonNull(sysLogVO.getEndDate())){
            criteria.andLessThan("createDate",sysLogVO.getEndDate());
        }

        example.orderBy("createDate").desc();
        List<SysLog> pageList = sysLogManager.getByExample(example);
        List<SysLogDTO> listDto = new ArrayList<>();
        if(!CollectionUtil.isNullOrEmpty(pageList)){
            listDto =  pageList.stream().map(e->{
                SysLogDTO dto = new SysLogDTO();
                BeanUtils.copyProperties(e,dto);
                return dto;
            }).collect(Collectors.toList());
        }
        return new PageInfo<>(listDto);
    }

    @Override
    public void sendEmailVerificationCode(SendEmailVO sendEmailVO,SessionModel sessionModel) {
        String randomNumer = RandomStringUtils.randomNumeric(6);
        Map<String,String> data = new HashMap<>();
        data.put("userName",sessionModel.getNickName());
        data.put("userId",sessionModel.getUserId().toString());
        data.put("newEmail",sendEmailVO.getNewEmail());
        data.put("oldEmail",sessionModel.getEmail());
        data.put("verificationCode",randomNumer);
        data.put("systemName","总管理平台");
        EmailModel emailModel = new EmailModel();
        emailModel.setAddress(sessionModel.getEmail());
        emailModel.setData(data);
        emailModel.setTemplateId(templateEmailVerificationCode);
        clientManager.sendEmail(emailModel);
        redisClient.set(RedisKeyPrefix.USER, "updateEmailVerificationCode"+sessionModel.getUserId(), randomNumer, userConfig.getEmailVerificationCodeExpire());

    }
}
